package org.zp.handler;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zp.dto.UpdateAddressDto;
import org.zp.pojo.AddressInfo;
import org.zp.pojo.ExpressInfo;
import org.zp.pojo.TypeInfo;
import org.zp.pojo.UserInfo;
import org.zp.service.AddressService;
import org.zp.service.ExpressInfoService;
import org.zp.service.TypeInfoService;
import org.zp.service.UserService;
import org.zp.util.Ajax;
import org.zp.util.Validate;

@RestController
public class OrdersHandler {
@Resource
private AddressService a;
@Resource
private TypeInfoService t;
@Resource
private UserService u;
@Resource
private ExpressInfoService e;
/**
 * 获得省的集合
 * @param req
 * @param resp
 */
@GetMapping("getaddressone.act")
public void getaddressone(HttpServletRequest req, HttpServletResponse resp) {
	List<AddressInfo> list = a.getaddressone();
	Ajax.success(resp, "list", list);
}
/**
 * 获得市的集合
 * @param fid
 * @param req
 * @param resp
 */
@GetMapping("getaddressTow.act")
public void getaddressTow(String fid,HttpServletRequest req, HttpServletResponse resp) {
	Integer Fid = Integer.parseInt(fid);
	List<AddressInfo> list = a.getaddressTow(Fid);
	Ajax.success(resp, "list", list);
}
/**
 * 获得区/县集合
 * @param fid
 * @param req
 * @param resp
 */
@GetMapping("getaddressThree.act")
public void getaddressThree(String fid,HttpServletRequest req, HttpServletResponse resp) {
	Integer Fid = Integer.parseInt(fid);
	List<AddressInfo> list = a.getaddressTow(Fid);
	Ajax.success(resp, "list", list);
}
@GetMapping("getType.act")
public void getType(HttpServletRequest req, HttpServletResponse resp) {
	Integer Fid = 0;
	List<TypeInfo> list = t.getType(Fid);
	Ajax.success(resp, "list", list);
}
@GetMapping("getType2.act")
public void getType2(String fid,HttpServletRequest req, HttpServletResponse resp) {
	Integer Fid = Integer.parseInt(fid);
	List<TypeInfo> list = t.getType(Fid);
	Ajax.success(resp, "list", list);
}
/**
 * 寄件
 * @param dto
 * @param req
 * @param resp
 */
@PostMapping("updateAddress.act")
public void updateAddress(UpdateAddressDto dto,HttpServletRequest req, HttpServletResponse resp) {
	//寄件人
	Integer one = 0;
	Integer tow = 0;
	Integer three = 0;
	try {
		one = Integer.parseInt(dto.getOne());
		tow = Integer.parseInt(dto.getTow());
		three = Integer.parseInt(dto.getThree());
	} catch (Exception e) {
		System.out.println("寄件值或空或转换失败");
	}
	String address = dto.getAddress();
	String eName = dto.geteName();
	System.out.println("寄件人区位:"+one+"-"+tow+"-"+three);
	System.out.println("寄件人地址:"+address);
	if (one==0||tow==0||three==0||address.equals("")) {
		Ajax.error(resp, "请补充完整寄件人地址信息");
		return;
	}
	if (eName.equals("")) {
		Ajax.error(resp, "请补充物品名");
		return;
	}
	
	
	//收件人
	Integer oneA = 0;
	Integer towB = 0;
	Integer threeC = 0;
	try {
		oneA = Integer.parseInt(dto.getOneA());
		towB = Integer.parseInt(dto.getTowB());
		threeC = Integer.parseInt(dto.getThreeC());
	} catch (Exception e) {
		// TODO: handle exception
		System.out.println("收件值或空或转换失败");
	}
	
	
	String address2 = dto.getAddress2();
	String shouName = dto.getShouName();
	String shouTell = dto.getShouTell();
	System.out.println("收件人区位:"+oneA+"-"+towB+"-"+threeC);
	System.out.println("收件人资料地址:"+address2+"---"+shouName+"---"+shouTell);
	if (oneA==0||towB==0||threeC==0||address2.equals("")||shouName.equals("")||shouTell.equals("")) {
		Ajax.error(resp, "请补充完整收件人地址信息");
		return;
	}
	//物品种类
	Integer good1 = -1;
	Integer good2 = -1;
	try {
	 good1 = Integer.parseInt(dto.getGoods1());
	 good2 = Integer.parseInt(dto.getGoods2());
	} catch (Exception e) {
		// TODO: handle exception
		System.out.println("物品值或空或转换失败");
	}
	System.out.println("物品种类:"+good1+"---"+good2);
	if (good1==-1||good2==-1) {
		Ajax.error(resp, "请补充完整物品信息");
		return;
	}
	
//对用户的地址进行更新
	//查询当前的用户对象
	UserInfo user = (UserInfo) req.getSession().getAttribute("user");
	Integer uid = 0;
	try {
		uid = user.getuId();
	} catch (Exception e) {
		Ajax.error(resp, "id不见了请重新登录");
		return;
	}
	Validate va = new Validate();
	if(!va.checkContent(address)) {
		Ajax.error(resp, va.getError(), 10000);
		return;
	}
	if(!va.checkContent(address2)) {
		Ajax.error(resp, va.getError(), 10000);
		return;
	}
	
	if(!va.checkTell(shouTell)) {
		Ajax.error(resp, va.getError(), 10000);
		return;
	}
	
	
	UserInfo use = u.getUserByuid(uid);
//获得该用户的余额
	Integer uMoney = use.getuMoney();
	int uMone = uMoney;
//首先要知道你钱是否有
	if (uMoney==0) {
		Ajax.error(resp, "余额不足了老铁，请充值");
		return;
	}
//其次你的钱够不够
	//获得你寄送的物品的运费
	TypeInfo tinfo = t.getTypebytid(good2);
	Integer tmMoney = tinfo.gettMoney();
	
	int tmMone = tmMoney;
	
	
	
	if (uMone>=tmMone) {
		
	//余额充足开始下单
		//接收市(寄件)
		use.setCity(tow);
		//接收区/县(寄件)
		use.setArea(three);
		//详细地址(寄件)
		use.setuAddress(address);
		//更新用户表的地址(寄件)
		Integer index = u.updateUserAddressInfo(use);
		
	//对快件目的地的地址更新
		//获得快递地址的对象
		ExpressInfo einfo = new ExpressInfo();
		//物品类型
		einfo.settId(good2);
		//寄件人姓名
		einfo.seteName(eName);
		//寄件人id
		einfo.setuId(uid);
		//详细地址(收件)
		einfo.setDest(address2);
		//接收市(收件)
		einfo.seteCity(towB);
		//接收区/县(收件)
		einfo.seteArea(threeC);
		//收件人姓名
		einfo.setaTtn(shouName);
		//收件人电话
		einfo.setaTtnTell(shouTell);
		//更新用户表的地址(收件)
		Integer index2 = e.insertExpress(einfo);
		
		//最后把钱扣除
		uMone =  uMone - tmMone;
		//将获得的积分修改它原来的余额
		Integer x = u.useraddMoney(uid,uMone);
		if(x>0) {
			System.out.println("余额修改成功");
			}else {
			System.out.println("余额修改失败");
			return;
		}
		
		
		
		if (index>0&&index2>0) {
		
			Ajax.success(resp);
			System.out.println("提交成功");
			//提交成功后查出这个快件的id
			Integer eid = e.selecyEid(einfo);
			System.out.println("该快件id为:"+eid);
			
			//将信息加入用户历史表
			Integer x1 = u.inserthistory(use,eid);
			if (x1>0) {
				System.out.println("--------------历史添加成功");
			}else {
				System.out.println("-------------历史添加失败");
				return;
			}
			
			return;
		}else {
			Ajax.error(resp, "提交失败",10001);
			System.out.println("提交失败");
		}
	}else {
		Ajax.error(resp, "余额不够噢，请充值");
		return;
	}
	
	
	
}
}
